package com.example.wxvoce.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wxvoce.R;
import com.example.wxvoce.bean.MusicBean;

import java.util.List;

//此处 继承的 RecyclerView.Adapter 中的泛型 指定为我们自定义的 ViewHOlder的类型
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.CzyViewHolder> {
    Context context;
    List<MusicBean> itemList;

    OnItemClick onItemClick;

    public MyAdapter(Context context, List<MusicBean> itemList) {
        this.context = context;
        this.itemList = itemList;
    }
    /** 创建 viewHolder 将 xml 传递给ViewHolder */
    @NonNull
    @Override
    public CzyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        CzyViewHolder czyViewHolder = new CzyViewHolder(view);
        return czyViewHolder;
    }
    /** 这里是设置item操作的地方 */
    @Override    public void onBindViewHolder(@NonNull CzyViewHolder czyViewHolder, int i) {
        czyViewHolder.name.setText((itemList.get(i).getName()));
        czyViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClick.onClick(i);
            }
        });
    }

    public void setOnItemClick(OnItemClick onItemClick){
        this.onItemClick =onItemClick;
    }

    /** 跟 BaseAdapter 的 getCount 作用一样。返回 */
    @Override
    public int getItemCount() {
        return itemList.size();
    }
    /** ViewHolder类，这个类用来初始化控件 */
    class CzyViewHolder extends RecyclerView.ViewHolder{
        TextView name;
        public CzyViewHolder(@NonNull View itemView) {
            super(itemView);

            name =itemView.findViewById(R.id.txt_item);

        }
    }

   public   interface  OnItemClick{
        void onClick(int i);
    }
}
